package com.stone.drawable;
import com.stone.R;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ComposePathEffect;
import android.graphics.CornerPathEffect;
import android.graphics.DiscretePathEffect;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PathEffect;
import android.graphics.RectF;
import android.graphics.Shader;
import android.graphics.SweepGradient;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.ArcShape;
import android.graphics.drawable.shapes.OvalShape;
import android.graphics.drawable.shapes.PathShape;
import android.graphics.drawable.shapes.RectShape;
import android.graphics.drawable.shapes.RoundRectShape;
import android.graphics.drawable.shapes.Shape;
import android.os.Bundle;
import android.view.View;
import android.widget.FrameLayout;
/**
 * 如何画出多个图形的处理
 * */
public class MultDrawables  extends Activity{
  @Override
 protected void onCreate(Bundle savedInstanceState) {
	// TODO Auto-generated method stub
	super.onCreate(savedInstanceState);
	setContentView(R.layout.drawabel1);
	FrameLayout fl=(FrameLayout)findViewById(R.id.FrameLayout01);
    fl.addView(new MyView(this));
  }
  private class MyView extends View{
    private ShapeDrawable[] mDrawables;
    
	public MyView(Context context) {
		super(context);
	    setFocusable(true);
	    mDrawables=new ShapeDrawable[7];
	    //画矩形
	    mDrawables[0]=new ShapeDrawable(new RectShape());
	    //在此处设置画笔的颜色。
	    mDrawables[0].getPaint().setColor(Color.RED);
	    
	    //画椭圆
	    mDrawables[1]=new ShapeDrawable(new OvalShape());
	    mDrawables[1].getPaint().setColor(Color.GREEN);
	   
	    //画出弧状矩形，8个float的矩阵依照顺时针的方向，分别
	    //用在左上角的X和y，右上角的X，y，右下角的X，y，左下角x，y
	    //画出圆的位置。
	    float[] outR=new float[]{12,12,12,12,0,0,0,0};
	   
	    float[] inner=new float[]{12,12,0,0,12,12,0,0};
	    mDrawables[2]=new ShapeDrawable(new RoundRectShape(outR, null, null));
	    mDrawables[2].getPaint().setColor(Color.BLUE);
	    //四点坐标定义矩形
	    RectF inset=new RectF(6,6,6,6);
	    //这里使用outR画出外围，inset画出内弧，可以使用现在的
	    //RectF类，也可以使用float[8]的数组。如果为null则不能画出内弧。
	    
	    mDrawables[3]=new ShapeDrawable(new RoundRectShape(outR, inset, null));
	   //设置着色器
	    mDrawables[3].getPaint().setShader(makeSweep());
	   //不连续的路径，将线条变成不连续的路径。
	    PathEffect pe=new DiscretePathEffect(10, 4);
	    //角度的变换方式
	    PathEffect pe2=new CornerPathEffect(4);
	    //设置组合路径效果
	    mDrawables[3].getPaint().setPathEffect(new ComposePathEffect(pe2, pe));
	    //椭圆形边而且中间空的矩形。
	    mDrawables[4]=new ShapeDrawable(new RoundRectShape(outR, inset, inner));
	    mDrawables[4].getPaint().setShader(makeLinear());
	    //画菱形
	    //设置路径
	    Path path=new Path();
	    path.moveTo(50, 0);
	    path.lineTo(0, 50);
	    path.lineTo(50, 100);
	    path.lineTo(100, 50);
	    path.close();
	    mDrawables[5]=new ShapeDrawable(new PathShape(path, 100, 100));
	    //mDrawables[5].getPaint().setColor(Color.YELLOW);
	     mDrawables[5].getPaint().setShader(makeTiling());
	     //继承ShapeDrawable,实现自定义形状。
	     mDrawables[6]=new MyShapeDrawable(new ArcShape(45, -270));
	     mDrawables[6].getPaint().setColor(0x88FF8844);
	     MyShapeDrawable msd=(MyShapeDrawable)mDrawables[6];
	     msd.getStrokePaint().setStrokeWidth(4);
	}
	//设定着色器Sweep［swiːp］扫描 Gradient['ɡreɪdiənt]斜度
	private  Shader makeSweep(){
		//在150,25的地方设定颜色，用红绿兰进行填充。
		return new SweepGradient(150, 25, new int[]{ 0xFFFF0000,0xFF00FF00,0xFF0000FF,0xFFFF0000}, null);
	}
	//第五个元素的着色器,设定颜色的效果是从红色到黑色，然后再从黑色到红色。
    private  Shader makeLinear() {
        return new LinearGradient(0, 0, 50, 50,
                          new int[] { 0xFFFF0000, 0xFF00FF00, 0xFF0000FF },
                          null, Shader.TileMode.MIRROR);
    }
    //继承ShapeDrawable，进行画笔的设置
    private  class MyShapeDrawable extends ShapeDrawable{
    	private Paint mStrokePaint=new Paint(Paint.ANTI_ALIAS_FLAG);
    	public MyShapeDrawable(Shape s){
    		super(s);
    		mStrokePaint.setStyle(Paint.Style.STROKE);
    	}
    	public Paint getStrokePaint(){
    		return mStrokePaint;
    	}
    	@Override
    	protected void onDraw(Shape shape, Canvas canvas, Paint paint) {
    		// TODO Auto-generated method stub
    		super.onDraw(shape, canvas, paint);
    		shape.draw(canvas, paint);
    		shape.draw(canvas, mStrokePaint);
    	}
    }
    //BitmapShader
    private Shader makeTiling(){
    	//设置createBitmap要绘制的颜色
    	int[] pixels=new int[]{0xFFFF0000,0xFF00FF00,0xFF0000FF,0};
    	Bitmap bm=Bitmap.createBitmap(pixels,2,2,Bitmap.Config.ARGB_8888);
    	//TileMode为重复
    	return new BitmapShader(bm, Shader.TileMode.REPEAT, Shader.TileMode.REPEAT);
    }
	  @Override
	protected void onDraw(Canvas canvas) {
		// TODO Auto-generated method stub
		super.onDraw(canvas);
		int x=10;
		int y=10;
		int width=300;
		int height=50;
		for(Drawable dr:mDrawables){
			 if(dr!=null){
				 //必须设置范围，否则无法显示
				 dr.setBounds(x, y, x+width,y+height);
				  //此处的canvas不能这样设置
				 // canvas.drawColor(Color.WHITE);
				 dr.draw(canvas);
			 }
			 y+=height+5;
		}
	}
  }
  
}
